package cn.mastercom.gui.components;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;

public class CustomDialog extends JDialog {

    private static final long serialVersionUID = 1L;
    private final JPanel contentPanel;
    private JTextArea textArea;


    public static void show(String msg) {
        JOptionPane.showMessageDialog(null, msg);
    }

    public static boolean show(String msg, String title) {
        return JOptionPane.showConfirmDialog(null, msg, title, JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION;
    }

    public static void main(String[] args) {
        show("<html>fsfsdfsdfdsf</html>");
    }

    private CustomDialog() {
        // 设置属性
        setTitle("提示");
        setBounds((Toolkit.getDefaultToolkit().getScreenSize().width - 266) / 2, (Toolkit.getDefaultToolkit().getScreenSize().height - 158) / 2, 266, 158);
        getContentPane().setLayout(new BorderLayout());
        setAlwaysOnTop(true);
        setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        setVisible(false);
        setResizable(false);

        // 初始化成员变量
        contentPanel = new JPanel();
        contentPanel.setLayout(new FlowLayout());
        contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));

        // 初始化非成员变量
        JPanel buttonPane = new JPanel();
        buttonPane.setLayout(new FlowLayout(FlowLayout.CENTER));
        JButton okButton = new JButton("OK");
        okButton.addActionListener(e -> dispose());
        okButton.setActionCommand("OK");

        // 添加组件
        getContentPane().add(contentPanel, BorderLayout.CENTER);

        textArea = new JTextArea();
        textArea.setLineWrap(true);
        textArea.setEditable(false);
        textArea.setColumns(5);
        textArea.setWrapStyleWord(true);
        textArea.setBackground(new Color(240, 240, 240));
        textArea.setAlignmentX(JTextArea.CENTER_ALIGNMENT);
        textArea.setAlignmentY(JTextArea.CENTER_ALIGNMENT);
        textArea.setPreferredSize(new Dimension(240, 50));
        contentPanel.add(textArea);

        getContentPane().add(buttonPane, BorderLayout.SOUTH);
        buttonPane.add(okButton);
        getRootPane().setDefaultButton(okButton);
    }

}
